package cuongnh.project.japanesediary.vocabulary.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cuongnh.project.japanesediary.vocabulary.model.Text;
import cuongnh.project.japanesediary.vocabulary.model.Vocabulary;

@Repository
@Transactional
public class TextDao extends HibernateDaoSupport {
	
	public void save(Text a1) {
		a1.setCreatedDate(new Date());
		a1.setModifiedDate(new Date());
		getHibernateTemplate().save(a1);
	}

	/**
	 * 'load' returns a proxy with only the id populated. To access members in
	 * the proxy requires a Hibernate session. Haven't figured out how to do
	 * transactions between bundles yet...
	 */
	public Text load(long id) {
		return (Text) getHibernateTemplate().load(Text.class, id);
	}

	public Text get(long id) {
		return (Text) getHibernateTemplate().get(Text.class, id);
	}

	public void update(Text a1) {
		a1.setModifiedDate(new Date());
		getHibernateTemplate().update(a1);
	}

	public void delete(Text a1) {
		getHibernateTemplate().delete(a1);
	}

	public Text merge(Text a1) {
		a1.setModifiedDate(new Date());
		return (Text) getHibernateTemplate().merge(a1);
	}
	
	public void saveOrUpdate(Text a1) {
		a1.setModifiedDate(new Date());
		if(a1.getCreatedDate() == null){
			a1.setCreatedDate(new Date());
		}
		getHibernateTemplate().saveOrUpdate(a1);
	}

	public List<Text> getAll() {
		return getHibernateTemplate().loadAll(Text.class);
	}

	public Text getTextWithKanjis(long textId) {
		Text text = getHibernateTemplate().get(Text.class,				
				textId);
		getHibernateTemplate().initialize(text.getKanjis());
		return text;
	}
	
	public Text getTextWithGrammars(long textId) {
		Text text = getHibernateTemplate().get(Text.class,
				textId);
		getHibernateTemplate().initialize(text.getGrammars());
		return text;
	}
	
	public Text getTextWithContexts(long textId) {
		Text text = getHibernateTemplate().get(Text.class,
				textId);
		getHibernateTemplate().initialize(text.getContexts());
		return text;
	}
	
	public Set<Vocabulary> getRelatedVocabularies(long textId) {
		Text text = getHibernateTemplate().get(Text.class,
				textId);
		getHibernateTemplate().initialize(text.getVocabularies());
		return text.getVocabularies();
	}
	
	public Text getTextWithRelateds(long textId) {
		Text text = getHibernateTemplate().get(Text.class,
				textId);
		getHibernateTemplate().initialize(text.getVocabularies());
		return text;
	}
	
	public Text getTextWithAllLazies(long textId) {
		Text text = getHibernateTemplate().get(Text.class,
				textId);
		getHibernateTemplate().initialize(text.getContexts());
		getHibernateTemplate().initialize(text.getVocabularies());
		getHibernateTemplate().initialize(text.getKanjis());
		getHibernateTemplate().initialize(text.getGrammars());
		return text;
	}
	
}
